package com.xxr.mom.ci.charging.service;

import cn.hutool.core.collection.CollectionUtil;
import com.xxr.mom.ci.charging.device.dto.PileInfo;
import com.xxr.mom.ci.charging.device.dto.StationInfo;
import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.device.entity.Pile;
import com.xxr.mom.ci.charging.device.entity.Station;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.charging.mapper.device.PileMapper;
import com.xxr.mom.ci.charging.mapper.device.StationMapper;
import com.xxr.mom.ci.common.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @description: DriveStationService
 * @date: 2024/8/11 18:53
 * @author: natsuki_kining
 * @version: 1.0
 */
@Service
public class DeviceStationService {

    @Autowired
    private GunMapper gunMapper;
    @Autowired
    private PileMapper pileMapper;
    @Autowired
    private StationMapper stationMapper;

    public StationInfo getStationInfo(String stationNumber) {
        Station station = stationMapper.selectByNumber(stationNumber);
        if (Objects.isNull(station)) {
            return null;
        }
        StationInfo stationInfo = JsonUtil.parseObject(JsonUtil.toJSONString(station), StationInfo.class);
        List<Pile> pileList = pileMapper.selectByStationId(station.getId());
        if (CollectionUtil.isEmpty(pileList)) {
            return stationInfo;
        }
        List<PileInfo> pileInfos = JsonUtil.parseArrayList(JsonUtil.toJSONString(pileList), PileInfo.class);
        stationInfo.setPileList(pileInfos);
        pileInfos.forEach(pileInfo -> {
            List<Gun> gunList = gunMapper.selectByPileId(pileInfo.getId());
            pileInfo.setGunList(gunList);
        });

        return stationInfo;
    }
}
